﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using RBS.Admin.Controllers;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Core.BLL;
using RBS.Core.Extension;

namespace RBS.Admin.Areas.CityAdmin.Controllers
{
    public class CityController : BaseController
    {
        //
        // GET: /CityAdmin/City/

        private readonly RBS.Core.BLL.SYS_Province _province = new SYS_Province();
        private readonly RBS.Core.BLL.SYS_City _city = new SYS_City();
        private readonly RBS.Core.BLL.SYS_County _county = new SYS_County();

        public ActionResult Index()
        {
            return View();
        }
         
        /// <summary>
        /// 获取省份
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public string GetProvince(int id = 0)
        {
            IList<object> list = new List<object>();
            list.Add(new
            {
                id = id,
                text ="省份",
                children = GetProvinceChildren(id)
            });
            string s = new JavaScriptSerializer().Serialize(list);
            s = s.Replace(",\"children\":[]", "");
            return s;
        }
        public IList<object> GetProvinceChildren(int parentId)
        { 
            DataTable dataTable = _province . GetList("").Tables[0];
            IList<object> list = new List<object>();
            foreach (DataRow c in dataTable.Rows)
            {
                list.Add(new
                {
                    id = c["Id"],
                    text = c["ProvinceName"] 
                });
            }
            return list;
        }  
        //城市
        [HttpPost]
        public JsonResult GetCityInfoList(int parentId)
        {
            var jsondata = new JsonData { Success = true };
            DataTable dataTable = _city.GetList(" ProvinceId=" + parentId).Tables[0];
            var list = new List<RBS.Core.Model.SYS_City>(); 
            if (dataTable != null)
            {
                list.AddRange(from DataRow row in dataTable.Rows select row.ConvertToEntity<RBS.Core.Model.SYS_City>());
            }
            jsondata.Data = list;
            return Json(jsondata);
        }
        [HttpPost]
        public JsonResult GetCountyInfoList(int parentId)
        {
            var jsondata = new JsonData { Success = true };
            DataTable dataTable = _county.GetList(" CityId=" + parentId).Tables[0];
            var list = new List<RBS.Core.Model.SYS_County>();
            if (dataTable != null)
            {
                list.AddRange(from DataRow row in dataTable.Rows select row.ConvertToEntity<RBS.Core.Model.SYS_County>());
            }
            jsondata.Data = list;
            return Json(jsondata);
        }

         /// <summary>
         /// 
         /// </summary>
         /// <param name="id"></param>
         /// <param name="pid"></param>
         /// <returns></returns>
        public ActionResult AddCity(int pid = 0, int cid=0)
        {
            //if(id=) 
            var model = new RBS.Core.Model.SYS_City
                {
                    ProvinceId = pid,
                    UpdateDate = DateTime.Now,
                    CreateDate = DateTime.Now,
                };
            if (cid != 0)
            {
                model = _city.GetModel(cid);
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult AddCity(FormCollection collection)
        {
            var jet = new JsonReturnMessages();
            var a = collection;
            var model = new RBS.Core.Model.SYS_City
            {
                CityName = collection["CityName"],
                ZipCode = collection["ZipCode"],
                ProvinceId = Convert.ToInt32(collection["ProvinceId"]),
                CreateDate = DateTime.Parse(collection["CreateDate"]),
                UpdateDate = DateTime.Now
            };
          
            if (collection["Id"] != "0")
            {
                model.Id = Convert.ToInt32(collection["Id"]);
                _city.Update(model);
                AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改:" + model.Id); //记录日志
            }
            else
            {
                _city.Add(model);
                AddAdminLog(RBSEnums.ActionEnum.Add.ToString(), "添加:" + model.Id); //记录日志
                jet.IsSuccess = true;
            }
            return Json(jet);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult AddCounty(int cid=0,int ccid=0)
        { 
            var model = new RBS.Core.Model.SYS_County()
            {
                CityId = cid,
                UpdateDate = DateTime.Now,
                CreateDate = DateTime.Now
            };
            if (ccid != 0)
            {
                model = _county.GetModel(ccid);
            }
            return View(model); 
        }

        [HttpPost]
        public ActionResult AddCounty(FormCollection collection)
        {
            var jet = new JsonReturnMessages();
            var a = collection;
            var model = new RBS.Core.Model.SYS_County
            {
                CountyName = collection["CountyName"], 
                CityId = Convert.ToInt32(collection["CityId"]),
                CreateDate = DateTime.Parse(collection["CreateDate"]),
                UpdateDate = DateTime.Now
            };

            if (collection["Id"] != "0")
            {
                model.Id = Convert.ToInt32(collection["Id"]);
                _county.Update(model);
                AddAdminLog(RBSEnums.ActionEnum.Edit.ToString(), "修改:" + model.Id); //记录日志
            }
            else
            {
                _county.Add(model);
                AddAdminLog(RBSEnums.ActionEnum.Add.ToString(), "添加:" + model.Id); //记录日志
                jet.IsSuccess = true;
            }
            return Json(jet);
        }
        [HttpPost]
        public ActionResult DictInfoDelete(int id,string type)
        {
            var jet = new JsonReturnMessages();
            if (type == "City")
            {
                //删除城市
                   jet.IsSuccess=_city.Delete(id); 
            }
            else if(type=="County")
            {
                //删除区域
                  jet.IsSuccess= _county.Delete(id); 
            } 
            return Json(jet);
        }



    }
}
